package com.lsfb.duishangbaos.MoneyManage;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 提现
 * Created by Administrator on 2017/5/9 0009.
 */

public class DrawMoney extends Fragment {

    /**
     * view对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    @ViewInject(R.id.aty_listview)
    private PullToRefreshListView listview;

    private List<MyDetailBean.ListBean> mList;

    private DrawMoneyAdapter adapter;

    private boolean IsRefresh = false;

    private int page = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_money, null);
        ViewInjectUtils.inject(this, view);
        LsfbEvent.getInstantiation().register2(this);

        mList = new ArrayList<>();
        adapter = new DrawMoneyAdapter(mactivity, R.layout.item_money2, mList);
        listview.setAdapter(adapter);

        listview.setMode(PullToRefreshBase.Mode.BOTH);
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 0;
                IsRefresh = true;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                IsRefresh = false;
                page++;
                getData();
            }
        });
        getData();

        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("cla", "1");//1 提现 ，2 收益
        hashMap.put("num", "15");
        hashMap.put("page", String.valueOf(page));
        new BaseInternet().getData_Get(URLString.MYDETAIL, hashMap, MyDetailBean.class, new MyDetailEvent2<MyDetailBean>(), true, true);
    }

    @EventAnnotation
    public void MyDetailEvent(MyDetailEvent2<MyDetailBean> event) {
        switch (event.getCode()) {
            case 200:
                if (IsRefresh) {
                    mList.clear();
                }
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            case 1002:
                if (IsRefresh) {
                    mList.clear();
                } else {
                    page--;
                    LsfbLog.e("上拉订单没更多数据" + event.getMsg());
                }
                adapter.notifyDataSetChanged();
                SN.SnackShow(listview, "暂无记录");
                break;
            default:
                mList.clear();
                page = 0;
                adapter.notifyDataSetChanged();
                SN.SnackShow(listview, event.getMsg());
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onStop() {
        super.onStop();
        LsfbEvent.getInstantiation().unregister2(this);
    }

}
